<?php
/**
 * 路劲配置
 * User: 叶华盛
 * Date: 2017/4/3
 * Time: 16:05
 */
namespace core;
class Router
{
    private $uri;
    private $param;
    private $controller;
    private $action;
    private static $module;

    public function __construct()
    {
        header('X-Powered-By:test');
        $this->get_uri();
        $this->uri  =   urldecode(trim($this->uri, '/'));
        $this->uri  =   explode('?', $this->uri);
        $default_router =   json_decode(DEFAULT_ROUTER,true);
        self::$module       =   DEFAULT_MODULE;
        $this->controller   =   $default_router['controllers'];
        $this->action = $default_router['action'];
        if (!empty($this->uri[1])) {
            parse_str($this->uri[1], $this->param);
        }

        $delimiter  =   preg_match('/\//',$this->uri[0])?'/':'-';
        $router = explode($delimiter, $this->uri[0]);
        if( count( $router ) )
        {
            //分组
            if (current($router)) {
                if( $router[0] == 'index'.EXT )
                    array_shift($router);
                if( !empty( $router ) )
                {
                    $modules = include_once(DIY_MODULE);
                    if (!empty($modules)) {
                        if (isset($modules[$router[0]])) {
                            self::$module = $modules[$router[0]]['module'];
                            array_shift($router);
                        }
                    }
                }
            }

            //controllers
            if (current($router)) {
                if (strpos($router[0], '-')) {
                    $tem = array();
                    $controller_tem = explode('-', $router[0]);
                    foreach ($controller_tem as $v) {
                        $tem[] = ucfirst($v);
                    }
                    $this->controller = implode('', $tem);
                } else {
                    $this->controller = $router[0];
                }
                array_shift($router);
            }
            //action
            if (current($router)) {
                if (strpos($router[0], '-')) {
                    $tem = array();
                    $action_tem = explode('-', $router[0]);
                    foreach ($action_tem as $v) {
                        $tem[] = ucfirst($v);
                    }
                    $this->action = implode('', $tem);
                } else {
                    $this->action = $router[0];
                }
                array_shift($router);
            }
            //将后面的作为参数
            $paramCount =   count($router);
            if( $paramCount )
            {
                foreach ( $router as $key=>$value )
                {
                    if( $key % 2 == 0 )
                        $this->param[$value]    =   $_GET[$value]   =   $key+1<$paramCount?$router[$key+1]:null;
                }
            }

        }
        Request::set('request', $_REQUEST);
        Request::set('get', $this->param);
        Request::set('post', $_POST);
    }

    /**
     * 获取uri
     */
    private function get_uri()
    {
        $uri    =   $_SERVER['REQUEST_URI'];
        $uri    =   str_replace('index.php/','',$uri);
        if( file_exists(DIY_URI) )
            $diyUrl =   include_once(DIY_URI);
        if( !empty( $diyUrl ) )
        {
            $diyUrl     =   unserialize($diyUrl);
            $k_diyUrl   =   array_flip($diyUrl);
            if( in_array($uri,$k_diyUrl) )
                $uri    =   $diyUrl[$uri];
        }
        $uri            =   str_replace(SUFFIX,'',$uri);
        $this->uri      =   $uri;
    }

    /**
     * 获取当前控制器名称
     * @return string   当前控制器名
     */
    public function getController()
    {
        return $this->controller;
    }

    /**
     * 获取当前模块名
     * @return string   当前模块分组名
     */
    public function getModule()
    {
        return self::$module;
    }

    /**
     * 获取当前操作方法名称
     * @return string   当前操作方法名称
     */
    public function getAction()
    {
        return $this->action;
    }

    /*
     * 重定向
     */

    public static function redirect($url = '')
    {
        header( 'HTTP/1.1 301 Moved Permanently' );
        if( is_array( $url ) )
            $url    =   HTTP_HOST.self::createUrl($url);
        if (strpos($url, 'http') === false) {
            $url = 'http://'.$url;
        }
        header('Location: ' . $url);
        exit;
    }

    /**
     * 生成url连接
     * @param array $url
     * @param array $param
     * @return string
     */
    public static function createUrl(array $url=array(),$param=array())
    {
        $uri    =   '';
        $Separate   =   URI_SEPARATE;
        $module =   !empty($url['mod'])?$url['mod']:'';
        if( !empty( $module ) && $module !== 'api' )
        {
            $modules    =   include_once(DIY_MODULE);
            if( !empty( $modules ) )
            {
                foreach( $modules as $k=>$v )
                {
                    if( $v['module']===$module )
                    {
                        $module =   $k;
                        break;
                    }
                }
            }
            $uri    .=  $module;
        }
        $controller =   !empty($url['ctl'])?$url['ctl']:'';
        if( !empty( $controller ) )
            $uri    .=  $uri?$Separate.$controller:$controller;
        $action     =   !empty($url['act'])?$url['act']:'';
        if( !empty( $action ) )
            $uri    .=  $Separate.$action;
        if( !empty( $param ) )
        {
            foreach( $param as $key=>$value )
            {
                if( !empty( $value ) )
                    $uri    .=  $Separate.$key.$Separate.$value;
            }
        }
        return $uri?DS.$uri.SUFFIX:'';
    }
}